/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Button;
import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.FocusListener;
import com.sun.lwuit.layouts.BoxLayout;
import core.NotificationManager;
import core.ScreenEngine;
import datamodel.Notification;
import view.additional.VolumeSetter;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class NotificationEditForm extends BasicForm
{  
    public static final int SAVE_COMMAND_ID = 0;
    public static final int TEST_BUTTON_COMMAND_ID = 1;
    
    private Notification notification;
    private Notification testNotification;
    
    private CheckBox cbVibration;
    private CheckBox cbBacklight;
    private RadioButton rbDurationTwo;
    private RadioButton rbDurationThirty;
    private RadioButton rbSoundNo;
    private RadioButton rbSoundBeep;
    private RadioButton rbSoundMelody;
    private VolumeSetter volume;
    
    public NotificationEditForm()
    {
        super("notif_settings");
        
        notification = null;
        testNotification = new Notification(0);
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setCyclicFocus(false);
        
        //Вибрация
        cbVibration = new CheckBox("vibration");
        cbVibration.setSelectCommandText("");
        addComponent(cbVibration);
        
        //Подсветка
        cbBacklight = new CheckBox("backlight");
        cbBacklight.setSelectCommandText("");
        addComponent(cbBacklight);
        
        //Время, через которое подсветка затухнет
        addComponent(new Label("backlight_duration"));
        ButtonGroup durationGroup = new ButtonGroup();
        
        rbDurationTwo = new RadioButton("duration_2");
        rbDurationTwo.setSelectCommandText("");
        durationGroup.add(rbDurationTwo);
        addComponent(rbDurationTwo);
        
        rbDurationThirty = new RadioButton("duration_30");
        rbDurationThirty.setSelectCommandText("");
        durationGroup.add(rbDurationThirty);
        addComponent(rbDurationThirty);
        
        //Звук
        addComponent(new Label("sound"));
        ButtonGroup group = new ButtonGroup();
        
        rbSoundNo = new RadioButton("no");
        rbSoundNo.setSelectCommandText("");
        group.add(rbSoundNo);
        addComponent(rbSoundNo);
        
        rbSoundBeep = new RadioButton("beep");
        rbSoundBeep.setSelectCommandText("");
        group.add(rbSoundBeep);
        addComponent(rbSoundBeep);
        
        rbSoundMelody = new RadioButton("melody");
        rbSoundMelody.setSelectCommandText("");
        group.add(rbSoundMelody);
        addComponent(rbSoundMelody);
        
        //Громкость
        addComponent(new Label("volume"));
        volume = new VolumeSetter();
        addFocusListener(new FocusListener() 
        {
            public void focusGained(Component cmpnt) 
            {
                if (cmpnt == volume)
                {             
                    volume.fireClicked();
                }
            }

            public void focusLost(Component cmpnt){}
        });
        addComponent(volume);

        //Кнопка "Пробовать"
        Command testButtonCommand = new Command("", TEST_BUTTON_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                testAction();
                
                evt.consume();
            }
        };
        Button testButton = new Button(testButtonCommand);
        testButton.setText("test");
        testButton.setSelectCommandText("");
        addComponent(testButton);
        
        //Команды
        addCommand(new BackCommand());
        
        Command save = new Command("save", SAVE_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {   
                //Сохраняем
                saveCurrentNotification();
                 
                ScreenEngine.getEngine().pop(true);
                
                evt.consume();
            }
        };
        addCommand(save);
    }

    public void setNotification(Notification notification) 
    {
        this.notification = notification;
    }
    
    public void show()
    {
        //Устанавливаем данные
        setContent();
        
        //Фокус на первый элемент переводим
        setFocused(cbVibration);
        scrollComponentToVisible(cbVibration);
        
        //Отображаем
        super.show();
    }
    
    private void saveCurrentNotification()
    {
        if (notification != null)
        { 
            notification.setVibration(cbVibration.isSelected());
            notification.setBacklight(cbBacklight.isSelected());
            if (rbDurationTwo.isSelected())
            {
                notification.setDuration(Notification.DURATION_TWO);
            }
            else if (rbDurationThirty.isSelected())
            {
                notification.setDuration(Notification.DURATION_THIRTY);
            }
            if (rbSoundNo.isSelected())
            {
                notification.setSound(Notification.SOUND_NO);
            }
            else if (rbSoundBeep.isSelected())
            {
                notification.setSound(Notification.SOUND_BEEP);
            }
            else if (rbSoundMelody.isSelected())
            {
                notification.setSound(Notification.SOUND_MELODY);
            }
            notification.setVolume(volume.getProgress());
            
            NotificationManager.getNotificationManager().save();
        }
    }
    
    private void setContent()
    {
        if (notification != null)
        {
            cbVibration.setSelected(notification.isVibration());
            cbBacklight.setSelected(notification.isBacklight());
            switch(notification.getDuration())
            {
                case Notification.DURATION_TWO : rbDurationTwo.setSelected(true); break;
                case Notification.DURATION_THIRTY : rbDurationThirty.setSelected(true); break;
            }
            switch(notification.getSound())
            {
                case Notification.SOUND_NO : rbSoundNo.setSelected(true); break;
                case Notification.SOUND_BEEP : rbSoundBeep.setSelected(true); break;
                case Notification.SOUND_MELODY : rbSoundMelody.setSelected(true); break;
            }
            volume.setProgress(notification.getVolume());
        }
    }
    
    private void testAction()
    {
        //Заполняем параметры
        if (notification != null)
        {
            testNotification.setId(notification.getId());
        }
        testNotification.setVibration(cbVibration.isSelected());
        testNotification.setBacklight(cbBacklight.isSelected());
        if (rbDurationTwo.isSelected())
        {
            testNotification.setDuration(Notification.DURATION_TWO);
        }
        else if (rbDurationThirty.isSelected())
        {
            testNotification.setDuration(Notification.DURATION_THIRTY);
        }
        if (rbSoundNo.isSelected())
        {
            testNotification.setSound(Notification.SOUND_NO);
        }
        else if (rbSoundBeep.isSelected())
        {
            testNotification.setSound(Notification.SOUND_BEEP);
        }
        else if (rbSoundMelody.isSelected())
        {
            testNotification.setSound(Notification.SOUND_MELODY);
        }
        testNotification.setVolume(volume.getProgress());

        //Запускаем
        testNotification.start();
    }
}
